﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Surfer;
using Pub;
using System.IO;

namespace SurferPub
{
    public struct TCurrQryCond
    {
        public string lvlFile;
        // 存储当前产品需要使用的色标文件
        public string Title;
        // 对应的产品标题
        public string MiddleBlankFile;
        // 绘图地图空文件
        public string MapFileName;
        // 绘图地图文件
        public double xmin;
        // 地图区域
        public double xmax;
        // 地图区域
        public double ymin;
        // 地图区域
        public double ymax;
    }

    public static class CSurferPub
    {
        // 
        // 功能：枚举当前文件路径下的所有文件
        // 参数：Path--文件夹全路径名   FileExt--枚举的扩展文件名匹配符
        // 说明：该函数利用递推的办法可以创建目标文件夹上一级文件夹不存在的文件夹
        //public static List<string> MakeFileList(string Path, string FileExt)
        //{
        //    List<string> result;
        //    TSearchrec sch;
        //    result = new List<string>();
        //    if (Path.Trim().Substring(0, Path.Trim().Length - 1) != "\\")
        //    {
        //        Path = Path.Trim() + "\\";
        //    }
        //    else
        //    {
        //        Path = Path.Trim();
        //    }
        //    if (!Directory.Exists(Path))
        //    {
        //        result.Clear();
        //        return result;
        //    }
        //    //@ Unsupported function or procedure: "FindFirst"
        //    if (FindFirst(Path + "*", 0x0000003f, sch) == 0)
        //    {
        //        do
        //        {
        //            //@ Unsupported property or method(C): "Name"
        //            //@ Unsupported property or method(C): "Name"
        //            if (((sch.Name == ".") || (sch.Name == "..")))
        //            {
        //                continue;
        //            }
        //            //@ Unsupported property or method(C): "Name"
        //            if (Directory.Exists(Path + sch.Name))
        //            {
        //                //@ Unsupported property or method(C): "Name"
        //                result.Add(MakeFileList(Path + sch.Name, FileExt));
        //            }
        //            else
        //            {
        //                //@ Unsupported property or method(C): "Name"
        //                if ((Path.GetExtension(Path + sch.Name).ToUpper() == FileExt.ToUpper()) || (FileExt == ".*"))
        //                {
        //                    //@ Unsupported property or method(C): "Name"
        //                    result.Add(Path + sch.Name);
        //                }
        //            }
        //            //@ Unsupported function or procedure: "FindNext"
        //        } while (!(FindNext(sch) != 0));
        //        //@ Unsupported function or procedure: "FindClose"
        //        FindClose(sch);
        //    }
        //    result.Sort();
        //    return result;
        //}

        // 
        // PrintSurferImg ：利用surf输出插值图像
        // tmpFileDir := ExtractFilePath( Application.ExeName ) + "tmp\";
        // StandardFileDir  := ExtractFilePath( Application.ExeName ) + "SrfStandard\";
        // lvlFile :ansiString = "";
        // MiddleBlankFilename : ansiString = "GANKONG.BLN" ;
        // MapFileName : ansistring = "GANMAP.BLN";
        // xmin,
        //public static void PrintSurferImg(string tmpFileDir, string StandardFileDir, string InDataFile, string OutImgFile, TCurrQryCond SurferParam, string imgWidth, string title, string DATfile)
        //{
        //    // 站点文件名
        //    string MiddleGrdFile_1;
        //    string MiddleGrdFile_2;
        //    string MiddleDataFile;
        //    string MiddleBlankFile;
        //    string MapFile;
        //    string MapStaFile;
        //    string LevelFile;
        //    string ExportImgFile;
        //    string ExportSurFile;
        //    object SurferApp;
        //    object Plot;
        //    object Shapes;
        //    object MapFrame_1;
        //    object MapFrame_2;
        //    object MapFrame_3;
        //    object MapFrame_text;
        //    object Map_1;
        //    object Map_2;
        //    object Map_3;
        //    object imagemap1;
        //    object Map_Text;
        //    object MapFrame_textEmpty;
        //    object colorscale1;
        //    object colormap1;
        //    // 
        //    // surf画图顺序（1、建立空白图，2、装载数据文件，3、白化 ，4、全选叠加，5、输出
        //    // 
        //    MiddleGrdFile_1 = tmpFileDir + "OutGrdFirst.grd";
        //    // 数据导入后生成的 grd 文件
        //    MiddleGrdFile_2 = tmpFileDir + "OutGrdSecond.grd";
        //    // 白化后产生的数据 grd 文件
        //    MiddleDataFile = InDataFile;
        //    // 需要标注的数据文件
        //    MiddleBlankFile = StandardFileDir + SurferParam.MiddleBlankFile;
        //    // 甘肃空地图
        //    MapFile = StandardFileDir + SurferParam.MapFileName;
        //    // 甘肃地图文件
        //    if (DATfile != "")
        //    {
        //        MapStaFile = StandardFileDir + DATfile;
        //    }
        //    // 甘肃标注站点文件
        //    ExportSurFile = tmpFileDir + "OutSrfFile.srf";
        //    // 导出的srf文件
        //    ExportImgFile = OutImgFile;
        //    // 导出的JPG文件
        //    if (SurferParam.lvlFile != "")
        //    {
        //        // 色标文件
        //        LevelFile = StandardFileDir + SurferParam.lvlFile;
        //    }
        //    else
        //    {
        //        LevelFile = "";
        //    }
        //    //@ Undeclared identifier(3): "CreateOleObject"
        //    SurferApp = CreateOleObject("surfer.application");
        //    try
        //    {
        //        //@ Unsupported property or method(C): "visible"
        //        SurferApp.visible = false;
        //        // SurferApp.windowstate :=  1;
        //        //@ Unsupported property or method(C): "Documents"
        //        //@ Unsupported property or method(B): "Add"
        //        Plot = SurferApp.Documents.Add(1);
        //        //@ Undeclared identifier(3): "datafile"
        //        //@ Undeclared identifier(3): "Algorithm"
        //        //@ Undeclared identifier(3): "DupMethod"
        //        //@ Undeclared identifier(3): "xCol"
        //        //@ Undeclared identifier(3): "yCol"
        //        //@ Undeclared identifier(3): "zCol"
        //        //@ Undeclared identifier(3): "xmin"
        //        //@ Undeclared identifier(3): "xmax"
        //        //@ Undeclared identifier(3): "ymin"
        //        //@ Undeclared identifier(3): "ymax"
        //        //@ Undeclared identifier(3): "xDupTol"
        //        //@ Undeclared identifier(3): "yDupTol"
        //        //@ Undeclared identifier(3): "ShowReport"
        //        //@ Undeclared identifier(3): "NumCols"
        //        //@ Undeclared identifier(3): "NumRows"
        //        //@ Undeclared identifier(3): "OutGrid"
        //        //@ Unsupported property or method(A): "griddata"
        //        SurferApp.griddata(datafile = MiddleDataFile, Algorithm = 2, DupMethod = 2, xCol = 3, yCol = 2, zCol = 5, xmin = SurferParam.xmin, xmax = SurferParam.xmax, ymin = SurferParam.ymin, ymax = SurferParam.ymax, xDupTol = 0.03, yDupTol = 0.03, ShowReport = false, NumCols = 540, NumRows = 360, OutGrid = MiddleGrdFile_1);
        //        // 白化当前数据
        //        //@ Undeclared identifier(3): "InGrid"
        //        //@ Undeclared identifier(3): "Blankfile"
        //        //@ Undeclared identifier(3): "OutGrid"
        //        //@ Unsupported property or method(A): "GridBlank"
        //        SurferApp.GridBlank(InGrid = MiddleGrdFile_1, Blankfile = MiddleBlankFile, OutGrid = MiddleGrdFile_2);
        //        //@ Unsupported property or method(C): "Shapes"
        //        Shapes = Plot.Shapes;
        //        if (LevelFile != "")
        //        {
        //            //@ Unsupported property or method(A): "AddContourMap"
        //            MapFrame_2 = Shapes.AddContourMap(MiddleGrdFile_2);
        //            //@ Unsupported property or method(C): "overlays"
        //            //@ Unsupported property or method(B): "item"
        //            Map_2 = MapFrame_2.overlays.item(1);
        //            //@ Unsupported property or method(C): "SmoothContours"
        //            Map_2.SmoothContours = 4;
        //            //@ Unsupported property or method(C): "FillContours"
        //            Map_2.FillContours = true;
        //            //@ Unsupported property or method(C): "ShowColorScale"
        //            Map_2.ShowColorScale = true;
        //            //@ Unsupported property or method(C): "levels"
        //            //@ Unsupported property or method(B): "LoadFile"
        //            Map_2.levels.LoadFile(LevelFile);
        //            //@ Unsupported property or method(C): "LabelFont"
        //            //@ Unsupported property or method(D): "Color"
        //            Map_2.LabelFont.Color = 0x0000FF;
        //            //@ Unsupported property or method(C): "LabelFont"
        //            //@ Unsupported property or method(D): "Size"
        //            Map_2.LabelFont.Size = 10;
        //            //@ Unsupported property or method(C): "LabelFont"
        //            //@ Unsupported property or method(D): "Face"
        //            Map_2.LabelFont.Face = "Times New Roman";
        //        }
        //        else
        //        {
        //            // 建立Image Map图像，并显示出色标
        //            //@ Unsupported property or method(A): "AddImageMap"
        //            MapFrame_2 = Shapes.AddImageMap(MiddleGrdFile_2);
        //            //@ Unsupported property or method(C): "Overlays"
        //            //@ Unsupported property or method(B): "item"
        //            imagemap1 = MapFrame_2.Overlays.item(1);
        //            //@ Unsupported property or method(C): "ShowColorScale"
        //            imagemap1.ShowColorScale = true;
        //            //@ Unsupported property or method(C): "ColorScale"
        //            colorscale1 = imagemap1.ColorScale;
        //            //@ Unsupported property or method(C): "Top"
        //            colorscale1.Top = 7.5;
        //            //@ Unsupported property or method(C): "Left"
        //            colorscale1.Left = 7.5;
        //            //@ Unsupported property or method(C): "ColorMap"
        //            colormap1 = imagemap1.ColorMap;
        //            //@ Unsupported property or method(A): "LoadFile"
        //            colormap1.LoadFile(StandardFileDir + "Rainbow2.CLR");
        //            //@ Unsupported property or method(C): "MissingDataColor"
        //            imagemap1.MissingDataColor = 0xFFFFFF;
        //        }
        //        // 加载站点信息
        //        if (MapStaFile != "")
        //        {
        //            // AddPostMap( DataFileName, xCol, yCol, LabCol, SymCol, AngleCol )
        //            //@ Unsupported property or method(A): "AddPostMap"
        //            MapFrame_3 = Shapes.AddPostMap(MapStaFile, 3, 2, 7, 6, 0);
        //            //@ Unsupported property or method(C): "overlays"
        //            //@ Unsupported property or method(B): "item"
        //            Map_3 = MapFrame_3.overlays.item(1);
        //            //@ Unsupported property or method(C): "LabelFont"
        //            //@ Unsupported property or method(D): "Face"
        //            Map_3.LabelFont.Face = "隶书";
        //            //@ Unsupported property or method(C): "LabelFont"
        //            //@ Unsupported property or method(D): "Size"
        //            Map_3.LabelFont.Size = 9;
        //            //@ Unsupported property or method(C): "Symbol"
        //            //@ Unsupported property or method(D): "Color"
        //            Map_3.Symbol.Color = 0x0000FF;
        //            //@ Unsupported property or method(C): "Symbol"
        //            //@ Unsupported property or method(D): "Size"
        //            Map_3.Symbol.Size = 0.04;
        //            //@ Unsupported property or method(C): "Symbol"
        //            //@ Unsupported property or method(D): "Index"
        //            Map_3.Symbol.Index = 12;
        //            //@ Unsupported property or method(C): "LabelPos"
        //            Map_3.LabelPos = 3;
        //            //@ Unsupported property or method(C): "LabelFont"
        //            //@ Unsupported property or method(D): "Color"
        //            Map_3.LabelFont.Color = 0xFF0000;
        //        }
        //        // MapFrame_text :=  Shapes.AddText(x:=2.3, y:=8.2, Text:= title )  ;
        //        //@ Undeclared identifier(3): "x"
        //        //@ Undeclared identifier(3): "y"
        //        //@ Unsupported property or method(A): "AddText"
        //        MapFrame_textEmpty = Shapes.AddText(x = 1.4, y = 8.5, System.IO.Stream = "     ");
        //        //@ Undeclared identifier(3): "x"
        //        //@ Undeclared identifier(3): "y"
        //        //@ Unsupported property or method(A): "AddText"
        //        MapFrame_text = Shapes.AddText(x = 1.4, y = 8.4, System.IO.Stream = title);
        //        //@ Unsupported property or method(C): "Font"
        //        //@ Unsupported property or method(D): "Face"
        //        MapFrame_text.Font.Face = "隶书";
        //        //@ Unsupported property or method(C): "Font"
        //        //@ Unsupported property or method(D): "Size"
        //        MapFrame_text.Font.Size = 14;
        //        //@ Unsupported property or method(C): "Shapes"
        //        //@ Unsupported property or method(B): "Item"
        //        //@ Unsupported property or method(B): "SetLimits"
        //        Plot.Shapes.Item("Map").SetLimits(SurferParam.xmin, SurferParam.xmax, SurferParam.ymin, SurferParam.ymax);
        //        // 导入甘肃地图
        //        //@ Undeclared identifier(3): "ImportFileName"
        //        //@ Unsupported property or method(A): "AddBaseMap"
        //        MapFrame_1 = Shapes.AddBaseMap(ImportFileName = MapFile);
        //        //@ Unsupported property or method(C): "Overlays"
        //        //@ Unsupported property or method(B): "Item"
        //        Map_1 = MapFrame_1.Overlays.Item(1);
        //        //@ Unsupported property or method(C): "Axes"
        //        //@ Unsupported property or method(B): "Item"
        //        //@ Unsupported property or method(D): "LabelFormat"
        //        //@ Unsupported property or method(D): "Postfix"
        //        MapFrame_1.Axes.Item(1).LabelFormat.Postfix = "E";
        //        //@ Unsupported property or method(C): "Axes"
        //        //@ Unsupported property or method(B): "Item"
        //        //@ Unsupported property or method(D): "LabelFormat"
        //        //@ Unsupported property or method(D): "Postfix"
        //        MapFrame_1.Axes.Item(3).LabelFormat.Postfix = "N";
        //        // 全选图层，覆盖
        //        //@ Unsupported property or method(C): "Selection"
        //        //@ Unsupported property or method(D): "DeselectAll"
        //        Plot.Selection.DeselectAll;
        //        //@ Unsupported property or method(C): "Selected"
        //        MapFrame_1.Selected = true;
        //        //@ Unsupported property or method(C): "Selected"
        //        MapFrame_2.Selected = true;
        //        if (MapStaFile != "")
        //        {
        //            //@ Unsupported property or method(C): "Selected"
        //            MapFrame_3.Selected = true;
        //        }
        //        //@ Unsupported property or method(C): "Selected"
        //        MapFrame_text.Selected = true;
        //        //@ Unsupported property or method(C): "Selection"
        //        //@ Unsupported property or method(D): "OverlayMaps"
        //        Plot.Selection.OverlayMaps;
        //        //@ Unsupported property or method(A): "SaveAs"
        //        Plot.SaveAs(ExportSurFile);
        //        //@ Undeclared identifier(3): "Filename"
        //        //@ Undeclared identifier(3): "Options"
        //        //@ Unsupported property or method(A): "Export"
        //        Plot.Export(Filename = ExportImgFile, Options = "Defaults=1,Width=" + imgWidth + ",KeepAspect=1,ColorDepth=24,Automatic=0,Quality=1000");
        //    }
        //    finally
        //    {
        //        //@ Unsupported property or method(C): "documents"
        //        //@ Unsupported property or method(D): "closeall"
        //        SurferApp.documents.closeall;
        //        //@ Unsupported property or method(C): "quit"
        //        SurferApp.quit;
        //    }
        //}

        //public static void PrintSurferImg(string tmpFileDir, string StandardFileDir, string InDataFile, string OutImgFile, TCurrQryCond SurferParam, string imgWidth, string title, string DATfile)
        public static void PrintSurferImg(string tmpFileDir, string StandardFileDir, string InDataFile, string OutImgFile, TCurrQryCond SurferParam, string imgWidth, string title, string DATfile)
        {
            string MiddleGrdFile_1;
            string MiddleGrdFile_2;
            string MiddleDataFile;
            string MiddleBlankFile;
            string MapFile;
            string MapStaFile;
            string LevelFile;
            string ExportImgFile;
            string ExportSurFile;
            Surfer.Application SurferApp;
            Surfer.IPlotDocument Plot;
            Surfer.IShapes Shapes;
            Surfer.IMapFrame MapFrame_1;
            Surfer.IMapFrame MapFrame_2;
            Surfer.IMapFrame MapFrame_3 = null;
            Surfer.IText MapFrame_text;
            Surfer.IPostMap Map_1;
            Surfer.IContourMap Map_2;
            Surfer.IPostMap Map_3;
            Surfer.IImageMap imagemap1;
            Surfer.IText Map_Text;
            Surfer.IContinuousColorScale colorscale1;
            Surfer.IColorMap colormap1;
            // 
            // surf画图顺序（1、建立空白图，2、装载数据文件，3、白化 ，4、全选叠加，5、输出
            // 
            MiddleGrdFile_1 = tmpFileDir + "OutGrdFirst.grd";
            // 数据导入后生成的 grd 文件
            MiddleGrdFile_2 = tmpFileDir + "OutGrdSecond.grd";
            // 白化后产生的数据 grd 文件
            MiddleDataFile = InDataFile;
            // 需要标注的数据文件
            MiddleBlankFile = StandardFileDir + SurferParam.MiddleBlankFile;
            // 甘肃空地图
            MapFile = StandardFileDir + SurferParam.MapFileName;
            // 甘肃地图文件
            MapStaFile = StandardFileDir + "LANZHOU.DAT";
            if (DATfile != "") MapStaFile = StandardFileDir + DATfile;

            // 甘肃标注站点文件
            ExportSurFile = tmpFileDir + "OutSrfFile.srf";
            // 导出的srf文件
            ExportImgFile = OutImgFile;
            // 导出的JPG文件
            if (SurferParam.lvlFile != "")
            {
                // 色标文件
                LevelFile = StandardFileDir + SurferParam.lvlFile;
            }
            else
            {
                LevelFile = "";
            }
            //@ Undeclared identifier(3): "CreateOleObject"
            SurferApp = new Surfer.Application(); // CreateOleObject("surfer.application");
            try
            {
                //@ Unsupported property or method(C): "visible"
                SurferApp.Application.Visible = false;

                // SurferApp.windowstate :=  1;
                //@ Unsupported property or method(C): "Documents"
                //@ Unsupported property or method(B): "Add"
                Plot = (Surfer.IPlotDocument)SurferApp.Application.Documents.Add(Surfer.SrfDocTypes.srfDocPlot);
                //Plot = SurferApp.Documents.Add(1);

                //@ Undeclared identifier(3): "datafile"
                //@ Undeclared identifier(3): "Algorithm"
                //@ Undeclared identifier(3): "DupMethod"
                //@ Undeclared identifier(3): "xCol"
                //@ Undeclared identifier(3): "yCol"
                //@ Undeclared identifier(3): "zCol"
                //@ Undeclared identifier(3): "xmin"
                //@ Undeclared identifier(3): "xmax"
                //@ Undeclared identifier(3): "ymin"
                //@ Undeclared identifier(3): "ymax"
                //@ Undeclared identifier(3): "xDupTol"
                //@ Undeclared identifier(3): "yDupTol"
                //@ Undeclared identifier(3): "ShowReport"
                //@ Undeclared identifier(3): "NumCols"
                //@ Undeclared identifier(3): "NumRows"
                //@ Undeclared identifier(3): "OutGrid"
                //@ Unsupported property or method(A): "griddata"

                //SurferApp.Application.GridData(MiddleDataFile, 2, 2, 3, 2,
                //    5, SurferParam.xmin, SurferParam.xmax, SurferParam.ymin, SurferParam.ymax,
                //    0.03, 0.03, false, 540, 360, MiddleGrdFile_1);

                //SurferApp.griddata(datafile = MiddleDataFile, Algorithm = 2, DupMethod = 2, xCol = 3, yCol = 2, zCol = 5, xmin = SurferParam.xmin, xmax = SurferParam.xmax, ymin = SurferParam.ymin, ymax = SurferParam.ymax, xDupTol = 0.03, yDupTol = 0.03, ShowReport = false, NumCols = 540, NumRows = 360, OutGrid = MiddleGrdFile_1);
                // 白化当前数据
                //@ Undeclared identifier(3): "InGrid"
                //@ Undeclared identifier(3): "Blankfile"
                //@ Undeclared identifier(3): "OutGrid"
                //@ Unsupported property or method(A): "GridBlank"

                //SurferApp.Application.GridBlank(MiddleGrdFile_1, MiddleBlankFile, MiddleGrdFile_2);

                //SurferApp.GridBlank(InGrid = MiddleGrdFile_1, Blankfile = MiddleBlankFile, OutGrid = MiddleGrdFile_2);
                //@ Unsupported property or method(C): "Shapes"

                /*
                bool GridData(string DataFile, int xCol, int yCol, int zCol, object ExclusionFilter, 
                  object DupMethod, object xDupTol, object yDupTol, object NumCols, object NumRows, 
                  object xMin, object xMax, object yMin, object yMax, object Algorithm, 
                  object ShowReport, object SearchEnable, object SearchNumSectors, object SearchRad1, object SearchRad2, 
                  object SearchAngle, object SearchMinData, object SearchDataPerSect, object SearchMaxEmpty, object FaultFileName, 
                  object BreakFileName, object AnisotropyRatio, object AnisotropyAngle, object IDPower, object IDSmoothing, 
                  object KrigType, object KrigDriftType, object KrigStdDevGrid, object KrigVariogram, object MCMaxResidual, 
                  object MCMaxIterations, object MCInternalTension, object MCBoundaryTension, object MCRelaxationFactor, object ShepSmoothFactor, 
                  object ShepQuadraticNeighbors, object ShepWeightingNeighbors, object ShepRange1, object ShepRange2, object RegrMaxXOrder, 
                  object RegrMaxYOrder, object RegrMaxTotalOrder, object RBBasisType, object RBRSquared, string OutGrid, 
                  SrfGridFormat OutFmt, object SearchMaxData, object KrigStdDevFormat, object DataMetric, object LocalPolyOrder, 
                  object LocalPolyPower, object TriangleFileName);
                */

                SurferApp.Application.GridData(MiddleDataFile, 3, 2, 5, Type.Missing,//1, 2, 4, Type.Missing, //5
                    2, 0.03f, 0.03f, 540, 360,  //10
                    SurferParam.xmin, SurferParam.xmax, SurferParam.ymin, SurferParam.ymax, 2, //15
                    false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //20
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //25
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //30
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //35
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //40
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //45
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, MiddleGrdFile_1, //50 
                    SrfGridFormat.srfGridFmtAscii, Type.Missing, Type.Missing, Type.Missing, Type.Missing, //55 
                    Type.Missing, Type.Missing
                    );
                SurferApp.Application.GridBlank(MiddleGrdFile_1, MiddleBlankFile, MiddleGrdFile_2, SrfGridFormat.srfGridFmtAscii);



                Shapes = Plot.Shapes;

                if (LevelFile != "")
                {
                    //@ Unsupported property or method(A): "AddContourMap"
                    MapFrame_2 = Shapes.AddContourMap(MiddleGrdFile_2);
                    //@ Unsupported property or method(C): "overlays"
                    //@ Unsupported property or method(B): "item"
                    Map_2 = (IContourMap)MapFrame_2.Overlays.Item(1);
                    //@ Unsupported property or method(C): "SmoothContours"


                    Map_2.SmoothContours = SrfConSmoothType.srfConSmoothHigh; //4;
                    //@ Unsupported property or method(C): "FillContours"
                    Map_2.FillContours = true;
                    //@ Unsupported property or method(C): "ShowColorScale"
                    Map_2.ShowColorScale = true;
                    //@ Unsupported property or method(C): "levels"
                    //@ Unsupported property or method(B): "LoadFile"
                    Map_2.Levels.LoadFile(LevelFile);
                    //@ Unsupported property or method(C): "LabelFont"
                    //@ Unsupported property or method(D): "Color"
                    Map_2.LabelFont.Color = Surfer.srfColor.srfColorBlue;   //ColorTranslator.FromHtml("#0000FF");
                    //@ Unsupported property or method(C): "LabelFont"
                    //@ Unsupported property or method(D): "Size"
                    Map_2.LabelFont.Size = 10;
                    //@ Unsupported property or method(C): "LabelFont"
                    //@ Unsupported property or method(D): "Face"
                    Map_2.LabelFont.Face = "Times New Roman";
                }
                else
                {
                    // 建立Image Map图像，并显示出色标
                    //@ Unsupported property or method(A): "AddImageMap"
                    MapFrame_2 = Shapes.AddImageMap(MiddleGrdFile_2);
                    //@ Unsupported property or method(C): "Overlays"
                    //@ Unsupported property or method(B): "item"
                    imagemap1 = (Surfer.IImageMap)MapFrame_2.Overlays.Item(1);
                    //@ Unsupported property or method(C): "ShowColorScale"
                    imagemap1.ShowColorScale = true;
                    //@ Unsupported property or method(C): "ColorScale"
                    colorscale1 = imagemap1.ColorScale;
                    //@ Unsupported property or method(C): "Top"
                    colorscale1.Top = 7.5;
                    //@ Unsupported property or method(C): "Left"
                    colorscale1.Left = 7.5;
                    //@ Unsupported property or method(C): "ColorMap"
                    colormap1 = imagemap1.ColorMap;
                    //@ Unsupported property or method(A): "LoadFile"
                    colormap1.LoadFile(StandardFileDir + "Rainbow2.CLR");
                    //@ Unsupported property or method(C): "MissingDataColor"
                    imagemap1.MissingDataColor = srfColor.srfColorWhite; //0xFFFFFF;
                }
                // 加载站点信息
                if (MapStaFile != "")
                {
                    //@ Unsupported property or method(A): "AddPostMap"
                    MapFrame_3 = Shapes.AddPostMap(MapStaFile, 3, 2, 7, 6, 0);
                    //@ Unsupported property or method(C): "overlays"
                    //@ Unsupported property or method(B): "item"
                    Map_3 = (IPostMap)MapFrame_3.Overlays.Item(1);
                    //@ Unsupported property or method(C): "LabelFont"
                    //@ Unsupported property or method(D): "Face"
                    Map_3.LabelFont.Face = "Times New Roman";
                    //@ Unsupported property or method(C): "LabelFont"
                    //@ Unsupported property or method(D): "Size"
                    Map_3.LabelFont.Size = 10;
                    //@ Unsupported property or method(C): "Symbol"
                    //@ Unsupported property or method(D): "Color"
                    Map_3.Symbol.Color = srfColor.srfColorBlue; //0x0000FF;
                    //@ Unsupported property or method(C): "Symbol"
                    //@ Unsupported property or method(D): "Size"
                    Map_3.Symbol.Size = 0.03;
                    //@ Unsupported property or method(C): "Symbol"
                    //@ Unsupported property or method(D): "Index"
                    Map_3.Symbol.Index = 12;
                    //@ Unsupported property or method(C): "LabelPos"
                    Map_3.LabelPos = SrfPostPosType.srfPostPosLeft; //2;
                    //@ Unsupported property or method(C): "LabelFont"
                    //@ Unsupported property or method(D): "Color"
                    Map_3.LabelFont.Color = srfColor.srfColorRed; //0xFF0000;
                }
                // MapFrame_text :=  Shapes.AddText(x:=2.3, y:=8.2, Text:= title )  ;
                //@ Undeclared identifier(3): "x"
                //@ Undeclared identifier(3): "y"
                //@ Unsupported property or method(A): "AddText"
                MapFrame_text = Shapes.AddText(1.6, 8.6, title);
                //@ Unsupported property or method(C): "Font"
                //@ Unsupported property or method(D): "Face"
                MapFrame_text.Font.Face = "宋体";
                //@ Unsupported property or method(C): "Font"
                //@ Unsupported property or method(D): "Size"
                MapFrame_text.Font.Size = 18;
                //@ Unsupported property or method(C): "Shapes"
                //@ Unsupported property or method(B): "Item"
                //@ Unsupported property or method(B): "SetLimits"
                ((IMapFrame)Plot.Shapes.Item("Map")).SetLimits(SurferParam.xmin, SurferParam.xmax, SurferParam.ymin, SurferParam.ymax);
                // 导入甘肃地图
                //@ Undeclared identifier(3): "ImportFileName"
                //@ Unsupported property or method(A): "AddBaseMap"
                MapFrame_1 = Shapes.AddBaseMap(MapFile, null);
                //@ Unsupported property or method(C): "Overlays"
                //@ Unsupported property or method(B): "Item"


                //Map_1 = (IPostMap)MapFrame_1.Overlays.Item(1);

                //@ Unsupported property or method(C): "Axes"
                //@ Unsupported property or method(B): "Item"
                //@ Unsupported property or method(D): "LabelFormat"
                //@ Unsupported property or method(D): "Postfix"
                MapFrame_1.Axes.Item(1).LabelFormat.Postfix = "E";
                //@ Unsupported property or method(C): "Axes"
                //@ Unsupported property or method(B): "Item"
                //@ Unsupported property or method(D): "LabelFormat"
                //@ Unsupported property or method(D): "Postfix"
                MapFrame_1.Axes.Item(3).LabelFormat.Postfix = "N";
                // 全选图层，覆盖
                //@ Unsupported property or method(C): "Selection"
                //@ Unsupported property or method(D): "DeselectAll"
                Plot.Selection.DeselectAll();
                //@ Unsupported property or method(C): "Selected"
                MapFrame_1.Selected = true;
                //@ Unsupported property or method(C): "Selected"
                MapFrame_2.Selected = true;
                if (MapStaFile != "")
                {
                    //@ Unsupported property or method(C): "Selected"
                    MapFrame_3.Selected = true;
                }
                //@ Unsupported property or method(C): "Selected"
                MapFrame_text.Selected = true;
                //@ Unsupported property or method(C): "Selection"
                //@ Unsupported property or method(D): "OverlayMaps"
                Plot.Selection.OverlayMaps();
                //@ Unsupported property or method(A): "SaveAs"
                Plot.SaveAs(ExportSurFile, null, SrfSaveFormat.srfSaveFormatUnknown);
                //@ Undeclared identifier(3): "Filename"
                //@ Undeclared identifier(3): "Options"
                //@ Unsupported property or method(A): "Export"
                Plot.Export(ExportImgFile, false, "Defaults=1,Width=" + imgWidth + ",KeepAspect=1,ColorDepth=24,Automatic=0,Quality=1000");
            }
            catch (Exception e)
            {
                CDebug.SendError(e.ToString());
            }
            finally
            {
                //@ Unsupported property or method(C): "documents"
                //@ Unsupported property or method(D): "closeall"
                SurferApp.Documents.CloseAll(SrfSaveTypes.srfSaveChangesYes);
                //@ Unsupported property or method(C): "quit"
                SurferApp.Quit();

            }
        }

        public static void PrintSurferImg(string tmpFileDir, string StandardFileDir, string InDataFile, string OutImgFile, TCurrQryCond SurferParam)
        {
            PrintSurferImg(tmpFileDir, StandardFileDir, InDataFile, OutImgFile, SurferParam, "800");
        }

        public static void PrintSurferImg(string tmpFileDir, string StandardFileDir, string InDataFile, string OutImgFile, TCurrQryCond SurferParam, string imgWidth)
        {
            PrintSurferImg(tmpFileDir, StandardFileDir, InDataFile, OutImgFile, SurferParam, imgWidth, "");
        }

        public static void PrintSurferImg(string tmpFileDir, string StandardFileDir, string InDataFile, string OutImgFile, TCurrQryCond SurferParam, string imgWidth, string title)
        {
            PrintSurferImg(tmpFileDir, StandardFileDir, InDataFile, OutImgFile, SurferParam, imgWidth, title, "");
        }

        public static string DrawSurfImg(string[,] P_Arr_data, int EndPos, string tmpFileDir, string StandardFileDir, TCurrQryCond CurrQryResult, string DayTitle, string DATfile)
        {
            string result;
            // 临时文件存储文件夹,只存放输出的临时文件
            // 绘制surf必须的地图文件存放文件夹
            // 结构体,存放必须的绘制图形信息
            int i;
            int j;
            string InDataFile;
            string OutImgFile;
            string tmpStrLines;
            List<string> DataStrList;
            DataStrList = new List<string>();
            try
            {
                InDataFile = "";
                OutImgFile = "";
                // 开始计算数据
                tmpStrLines = "";
                // 0 : 站号 ; 1: 经度 ; 2: 纬度; 3 : 高程
                for (i = 4; i <= EndPos; i++)
                {
                    // MemoSurf.Lines.Clear ;
                    DataStrList.Clear();
                    tmpStrLines = "";
                    for (j = 0; j < P_Arr_data.GetLength(1); j++)
                    {
                        tmpStrLines = tmpStrLines + P_Arr_data[0, j] + "," + P_Arr_data[1, j] + "," + P_Arr_data[2, j] + "," + P_Arr_data[3, j] + "," + P_Arr_data[i, j] + "\r\n";
                        // 
                        // MemoSurf.Lines.Add( P_Arr_data[ 0 , j] + "," + P_Arr_data[1 , j]
                        // + "," +P_Arr_data[ 2 , j]  + ","
                        // + P_Arr_data[ 3 , j]  + ","
                        // +   P_Arr_data[ i , j] +  #13#10 ) ;
                        // 
                        if (P_Arr_data[0, j] != null && P_Arr_data[0, j] != string.Empty)
                            DataStrList.Add(P_Arr_data[0, j] + "," + P_Arr_data[1, j] + "," + P_Arr_data[2, j] + "," + P_Arr_data[3, j] + "," + P_Arr_data[i, j]);
                    }
                    // MemoSurf.Lines.Clear ;
                    // MemoSurf.Text := tmpStrLines ;
                    InDataFile = tmpFileDir + DayTitle + ".txt";
                    OutImgFile = tmpFileDir + DayTitle + ".bmp";
                    if (File.Exists(OutImgFile)) File.Delete(OutImgFile);
                    //@ Unsupported property or method(A): "SaveToFile"
                    //DataStrList.SaveToFile(InDataFile);
                    CFile.Write(InDataFile, DataStrList, Encoding.Default, true);
                    PrintSurferImg(tmpFileDir, StandardFileDir, InDataFile, OutImgFile, CurrQryResult, "700", DayTitle, DATfile);
                }
                // imgSurf.Picture.LoadFromFile(OutImgFile);
                result = OutImgFile;
            }
            catch (Exception e)
            {
                // 
                // on E:Exception do
                // begin
                // //ShowMessage("Error!");
                // end;
                // 
                CDebug.SendError(e.ToString());
                result = "";
            }
            //@ Unsupported property or method(C): "Free"
            //DataStrList.Free;
            DataStrList.Clear();
            return result;
        }

        public static string DrawSurfImg(string[,] P_Arr_data, int EndPos, string tmpFileDir, string StandardFileDir, TCurrQryCond CurrQryResult, string DayTitle)
        {
            return DrawSurfImg(P_Arr_data, EndPos, tmpFileDir, StandardFileDir, CurrQryResult, DayTitle, "");
        }

        public static void MakeLevelFile(string AppPath, double HighVal, double LowVal, string LevelFile, string LevelFileTemplate)
        {
            int i;
            int j;
            int Position;
            double m;
            double cnt;
            List<string> LevelFileList;
            LevelFileList = new List<string>();
            //@ Unsupported property or method(A): "LoadFromFile"
            //LevelFileList.LoadFromFile(AppPath + "SrfStandard\\" + LevelFileTemplate);
            CFile.Read(AppPath + "SrfStandard\\" + LevelFileTemplate, ref LevelFileList);
            cnt = HighVal - LowVal;
            //@ Unsupported function or procedure: "RoundTo"
            m = Math.Round(cnt / (LevelFileList.Count - 3), 1);
            for (i = 2; i < LevelFileList.Count; i++)
            {
                j = i - 2;
                Position = LevelFileList[i].IndexOf(" ");
                //@ Unsupported function or procedure: "Copy"
                LevelFileList[i] = Convert.ToString(LowVal + m * j) + LevelFileList[i].Substring(Position, LevelFileList[i].Length - Position);
            }
            // Position := Pos(" ", LevelFileList[2]);
            // LevelFileList[2] := FloatToStr(-m) + Copy(LevelFileList[2], Position, Length(LevelFileList[2]) - Position + 1);
            //@ Unsupported property or method(A): "SaveToFile"
            //LevelFileList.SaveToFile(AppPath + "SrfStandard\\" + LevelFile);
            CFile.Write(AppPath + "SrfStandard\\" + LevelFile, LevelFileList, Encoding.Default, true);
            //@ Unsupported property or method(C): "Free"
            //LevelFileList.Free;
            LevelFileList.Clear();
        }

        public static int DrawExcute(string DataFile)
        {
            int result = 0;
            string[,] P_DataList;
            List<string> StrList;
            //List<string> StrLineList;
            int i;
            int EndPos;
            TCurrQryCond CurrQryResult = new TCurrQryCond();
            string tmpFileDir;
            string StandardFileDir;
            string OutImgFile;
            string DayTitle;
            string SavePath;
            string StationDAT;
            //Image jp;
            //PictureBox imgSurf;
            StrList = new List<string>();
            //StrLineList = new List<string>();
            try
            {
                //@ Undeclared identifier(3): "TEncoding"
                //@ Unsupported property or method(D): "Default"
                //@ Unsupported property or method(A): "LoadFromFile"
                //StrList.LoadFromFile(DataFile, TEncoding.Default);
                CFile.Read(DataFile, ref StrList);
                P_DataList = new string[5, StrList.Count];
                DayTitle = StrList[13];
                DayTitle = DayTitle.Replace("\\", "＼");
                DayTitle = DayTitle.Replace("/", "／");
                EndPos = 4;
                tmpFileDir = StrList[0] + "tmp\\";
                StandardFileDir = StrList[0] + "SrfStandard\\";
                //@ Undeclared identifier(3): 'CDebug'
                //@ Unsupported property or method(B): 'EnterMethod'
                CDebug.EnterMethod("输出模版内容");
                for (i = 0; i < StrList.Count; i++)
                {
                    //@ Undeclared identifier(3): 'CDebug'
                    //@ Unsupported property or method(B): "Send"
                    //CDebug.Send(StrList[i]);
                }
                //@ Undeclared identifier(3): "CDebug"
                //@ Unsupported property or method(B): "ExitMethod"
                CDebug.ExitMethod("输出模版内容");
                for (i = 14; i < StrList.Count; i++)
                {
                    // 文件内数据提取
                    //StrLineList.Clear();
                    //@ Unsupported function or procedure: "ExtractStrings"
                    //ExtractStrings(new char[] { ',' }, new object[] { }, (StrList[i] as string), StrLineList);

                    string[] StrLineList = StrList[i].Split(',');
                    if (StrLineList.Length < 5)
                    {
                        break;
                    }
                    // 单行数据不完整跳过
                    P_DataList[0, i - 13] = StrLineList[0];
                    P_DataList[1, i - 13] = StrLineList[1];
                    P_DataList[2, i - 13] = StrLineList[2];
                    P_DataList[3, i - 13] = StrLineList[3];
                    P_DataList[4, i - 13] = StrLineList[4];
                }
                if (StrList[3] != "0")
                {
                    // 是否为动态色标
                    MakeLevelFile(StrList[0], Convert.ToDouble(StrList[4]), Convert.ToDouble(StrList[5]), StrList[6], StrList[3]);
                }
                CurrQryResult.lvlFile = StrList[6];
                // 色标文件, 为空,随即指定
                CurrQryResult.MiddleBlankFile = StrList[7];
                CurrQryResult.MapFileName = StrList[8];
                CurrQryResult.xmin = Convert.ToSingle(StrList[9]);
                CurrQryResult.xmax = Convert.ToSingle(StrList[10]);
                CurrQryResult.ymin = Convert.ToSingle(StrList[11]);
                CurrQryResult.ymax = Convert.ToSingle(StrList[12]);
                StationDAT = StrList[2];
                // CopyFile(PChar(StationDAT), PChar(StandardFileDir + "jiuquan.dat"), False);
                OutImgFile = DrawSurfImg(P_DataList, EndPos, tmpFileDir, StandardFileDir, CurrQryResult, DayTitle, StationDAT);
                if (OutImgFile == "")
                {
                    result = -1;
                    //@ Unsupported property or method(C): "Free"
                    StrList.Clear();
                    //@ Unsupported property or method(C): "Free"
                    //StrLineList.Free;
                    return result;
                }
                SavePath = StrList[1];
                if (!Directory.Exists(Path.GetDirectoryName(SavePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(SavePath));

                    result = -2;
                    //@ Unsupported property or method(C): "Free"
                    StrList.Clear();
                    //@ Unsupported property or method(C): "Free"
                    //StrLineList.Free;
                    return result;

                }
                // //转换为jpg格式图片
                // imgSurf := TImage.Create(nil);
                // imgSurf.Picture.LoadFromFile(OutImgFile);
                // jp := TJPEGImage.Create;
                // try
                // jp.Assign(imgSurf.Picture.Bitmap);
                // jp.SaveToFile(SavePath);
                // except
                // Result := -3;
                // jp.Free;
                // imgSurf.Free;
                // Exit;
                // end;
                // 
                if (File.Exists(SavePath)) File.Delete(SavePath);
                File.Copy(OutImgFile, SavePath);

                //@ Unsupported property or method(C): "Free"
                StrList.Clear();
                //@ Unsupported property or method(C): "Free"
                //StrLineList.Free;
                return result;

                // 
                // if not DeleteFile(DataFile) then
                // begin
                // Result := -4;
                // end;
                // 
                //result = 0;
            }
            catch (Exception e)
            {
                CDebug.SendError(e.ToString());
            }
            finally
            {
                //@ Unsupported property or method(C): "Free"
                StrList.Clear();
                //@ Unsupported property or method(C): "Free"
                //StrLineList.Free;

            }
            return result;
        }

        public static int MakeSurfImg(string DataFile)
        {
            int result;
            //@ Undeclared identifier(3): "CDebug"
            //@ Unsupported property or method(B): "EnterMethod"
            CDebug.EnterMethod("开始绘图");
            //@ Undeclared identifier(3): "CDebug"
            //@ Unsupported property or method(B): "Send"
            CDebug.Send(DataFile);
            if (File.Exists(DataFile))
            {
                //@ Undeclared identifier(3): "CDebug"
                //@ Unsupported property or method(B): "ExitMethod"
                CDebug.ExitMethod("结束绘图");
                result = DrawExcute(DataFile);
            }
            else
            {
                //@ Undeclared identifier(3): "CDebug"
                //@ Unsupported property or method(B): "ExitMethod"
                CDebug.ExitMethod("结束绘图");
                result = -5;
            }
            return result;
        }

        public static void MainEntry(string[] args)
        {
        }
    }
}
